package com.ucar.training.service.impl;

import com.ucar.training.common.ResponseEnum;
import com.ucar.training.common.Result;
import com.ucar.training.dao.IUserDao;
import com.ucar.training.dao.impl.UserDaoImpl;
import com.ucar.training.domain.User;
import com.ucar.training.service.IUserService;

import java.util.List;

/**
 * @author XiongMM
 * @date 2019/7/18 17:35
 * @description 业务层
 */
public class UserServiceImpl implements IUserService {

    private IUserDao userDao = new UserDaoImpl();

    @Override
    public Result registerUser(User user){
        User tmpUser = userDao.select(user.getUserName());
        if (tmpUser != null ) {
            return new Result(ResponseEnum.FAIL,"用户名已经存在");
        }
        userDao.save(user);
        return new Result(ResponseEnum.SUCCESS,"用户已经保存");
    }

    @Override
    public User loginUser(String userName, String userPwd) {
        return userDao.find(userName, userPwd);
    }

    @Override
    public List<User> selectAll() {
        return userDao.getAllUser();
    }

    @Override
    public Result delUser(String userName) {
         if(userDao.delOne(userName)){
             return new Result(ResponseEnum.SUCCESS,"删除成功");
         }else{
             return new Result(ResponseEnum.FAIL,"删除失败，原因：[数据库中不存在："+userName+" 用户]");
         }
    }

    @Override
    public User selectOne(String userName) {
        return userDao.select(userName);
    }

    @Override
    public void update(User user) {
        userDao.modifyUser(user);
    }
}
